<?php

/**
 * manage base action
 * login / logout and something
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class SiteController extends CController {

    public function actionLogin() {
	$model = new LoginForm;

	//check post
	if (isset($_POST['LoginForm'])) {
	    $model->attributes = $_POST['LoginForm'];

	    //validate and login
	    if ($model->validate() && $model->login()) {
		//update last login
		$user = user()->getModel();
		$user->last_login_time = time();
		$user->last_login_ip = r()->userHostAddress;

		$user->save();

		//redirect to backend
		//$this->redirect(array('dashboard/index'));
		$this->redirect(array('setting/index'));
	    } else {
		//print_r($model->getErrors());
	    }
	}

	$this->layout = 'login';
	$this->render('login', compact('model'));
    }

    /**
     * logout
     */
    public function actionLogout() {
	Yii::app()->user->logout();
	//redirect to home page
	$this->redirect(array('login'));
    }

    /**
     * Display page 
     */
    public function actionPage($id) {
	$aPage = Yii::app()->db->createCommand('SELECT * FROM {{pages}} WHERE page_id=' . $id)->queryRow();
	$this->render('page', array("aPage" => $aPage));
    }

}

